package org.ss.psci.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.ss.psci.entity.*;
import org.ss.psci.util.DateUtil;
import org.ss.psci.util.JsonUtil;
import org.ss.psci.util.ListRange;

public class ProjectService {

	private BaseDAO baseDAO;
	private List list;

	public static void main(String[] a) {
		new ProjectService().getConSeq();
	}

	private String getConSeq() {
		String seq = "";
		String date = new DateUtil().getDateString().replaceAll("-", "")
				.replaceAll(":", "");
		String time = new DateUtil().getTimeString().replaceAll("-", "")
				.replaceAll(":", "");
		System.out.println("HT" + date + time);
		return "HT" + date + time;
	}

	public void saveNewPmBid(Map formMap) {
		PmBid pb = new PmBid();
		try {
			BeanUtils.populate(pb, formMap);
			System.out.print(">>>" + pb.getType());
			if (pb.getType() != null) {
				pb.setType(new Integer(pb.getType()));
			} else {
				pb.setType(new Integer(1));
			}
			pb.setStatus(new Integer(0));
			baseDAO.getPmBidDAO().save(pb);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void saveProject(Map formMap) {
		PmProjects pb = new PmProjects();
		try {
			BeanUtils.populate(pb, formMap);
			baseDAO.getPmProjectsDAO().save(pb);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void savePmContracts(HttpServletRequest request, Map formMap) {
		PmContracts pb = new PmContracts();
		try {
			BeanUtils.populate(pb, formMap);
			String projectsId = request.getSession().getAttribute("projectsId")
					.toString();
			String username = request.getSession().getAttribute("username")
			.toString();
			pb.setProjectId(projectsId);
			pb.setConsId(getConSeq());
			pb.setStatus("0");
			pb.setUsername(username);
			baseDAO.getPmContractsDAO().save(pb);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// **************************************Project*****************************************

	public ListRange getPmProjects(HttpServletRequest request, Map condition) {
		String name = "";
		String number = "";
		String company = "";
		String manager = "";
		int rows, start, limit;
		try {
			String projectsId = request.getSession().getAttribute("projectsId")
					.toString();
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
				name = parseString(condition.get("name"));
				number = parseString(condition.get("number"));
				company = parseString(condition.get("company"));
				manager = parseString(condition.get("manager"));
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				if (name != null && (!"".equals(name))) {
					list = baseDAO.getPmProjectsDAO().findByName(name,
							projectsId);
				} else if (company != null && (!"".equals(company))) {
					list = baseDAO.getPmProjectsDAO().findByCompany(company,
							projectsId);
				} else if (number != null && (!"".equals(number))) {
					list = baseDAO.getPmProjectsDAO().findByNumber(number,
							projectsId);
				} else if (manager != null && (!"".equals(manager))) {
					list = baseDAO.getPmProjectsDAO().findByManager(manager,
							projectsId);
				} else {
					list = baseDAO.getPmProjectsDAO().findByProjectID(
							projectsId);
				}
			}
			List result = new ArrayList();
			if (list == null || list.size() == 0) {
				ListRange listRange = new ListRange();
				listRange.setData(result.toArray());
				listRange.setTotalSize(0);
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}

			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmProject(String temp, Map formMap) {
		PmProjects pu = new PmProjects();
		try {
			BeanUtils.populate(pu, formMap);
			PmProjects p = getPmProject(temp);

			if (p != null) {
				p = baseDAO.getPmProjectsDAO().copyEntity(p, pu);
				baseDAO.getPmProjectsDAO().save(p);
			} else {
				baseDAO.getPmProjectsDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmProject(String temp) {
		try {
			PmProjects p = getPmProject(temp);
			if (p != null) {
				baseDAO.getPmProjectsDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmProjects getPmProject(String temp) {
		PmProjects p = new PmProjects();
		List uList = baseDAO.getPmProjectsDAO().findByName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmProjects) uList.get(0);
			}
		}
		return p;
	}

	public List getPmConProcessByConsID(String consId, String projectId) {
		PmConProcess p = new PmConProcess();
		List uList = baseDAO.getPmConProcessDAO().findByConsIDAndProjectId(
				consId, projectId);
		if (uList != null) {
			if (uList.size() != 0) {
				return uList;
			}
		}
		return new ArrayList();
	}

	public List getPmConDepByConsID(String consId, String projectId) {
		PmConDep p = new PmConDep();
		List uList = baseDAO.getPmConDepDAO().findByConsIDAndProjectId(consId,
				projectId);
		if (uList != null) {
			if (uList.size() != 0) {
				return uList;
			}
		}
		return new ArrayList();
	}

	public List getPmProjectByCompanyID(String temp) {
		PmProjects p = new PmProjects();
		List uList = baseDAO.getPmProjectsDAO().findByCompanyID(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				return uList;
			}
		}
		return new ArrayList();
	}

	public PmProjects getPmProjectByProjectsId(String temp) {
		PmProjects p = new PmProjects();
		List uList = baseDAO.getPmProjectsDAO().findByProjectID(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				return (PmProjects) uList.get(0);
			}
		}
		return null;
	}

	// **************************************Bid*****************************************
	private String parseString(Object o) {
		if (o == null) {
			return "";
		}
		return o.toString();
	}

	public ListRange getPmBids(Map condition) {
		String projectName = "";
		String bidSeq = "";
		String bidTime = "";
		String manager = "";

		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
				projectName = parseString(condition.get("projectName"));
				bidSeq = parseString(condition.get("bidSeq"));
				bidTime = parseString(condition.get("bidTime"));
				manager = parseString(condition.get("manager"));

			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				if (projectName != null && (!"".equals(projectName))) {
					list = baseDAO.getPmBidDAO().findByProjectName(projectName);
				} else if (bidSeq != null && (!"".equals(bidSeq))) {
					list = baseDAO.getPmBidDAO().findByBidSeq(bidSeq);
				} else if (bidTime != null && (!"".equals(bidTime))) {
					list = baseDAO.getPmBidDAO().findByBidTime(bidTime);
				} else if (manager != null && (!"".equals(manager))) {
					list = baseDAO.getPmBidDAO().findByManager(manager);
				} else {
					list = baseDAO.getPmBidDAO().findAll();
				}
			}
			List result = new ArrayList();
			if (list == null || list.size() == 0) {
				ListRange listRange = new ListRange();
				listRange.setData(result.toArray());
				listRange.setTotalSize(0);
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}

			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmBid(String temp, Map formMap) {
		PmBid pu = new PmBid();
		try {
			BeanUtils.populate(pu, formMap);
			PmBid p = getPmBid(temp);

			if (p != null) {
				p = baseDAO.getPmBidDAO().copyEntity(p, pu);
				baseDAO.getPmBidDAO().save(p);
			} else {
				baseDAO.getPmBidDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmBid(String temp) {
		try {
			PmBid p = getPmBid(temp);
			if (p != null) {
				baseDAO.getPmBidDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmBid getPmBid(String temp) {
		PmBid p = new PmBid();
		List uList = baseDAO.getPmBidDAO().findByProjectName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmBid) uList.get(0);
			}
		}
		return p;
	}

	// **************************************PmContracts*****************************************
	public List getPmContracts(String projectsId) {
		return baseDAO.getPmContractsDAO().findByProjectId(projectsId);
	}

	public ListRange getPmContracts(HttpServletRequest request, Map condition) {
		String consName = "";
		String consId = "";
		String signDate = "";
		String jiaFang = "";
		String yiFang = "";
		int rows, start, limit;
		try {
			String projectsId = request.getSession().getAttribute("projectsId")
					.toString();
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
				consName = parseString(condition.get("consName"));
				consId = parseString(condition.get("consId"));
				signDate = parseString(condition.get("signDate"));
				jiaFang = parseString(condition.get("jiaFang"));
				yiFang = parseString(condition.get("yiFang"));
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				if (consName != null && (!"".equals(consName))) {
					list = baseDAO.getPmContractsDAO().findByConsName(consName,
							projectsId);
				} else if (consId != null && (!"".equals(consId))) {
					list = baseDAO.getPmContractsDAO().findByConsId(consId,
							projectsId);
				} else if (signDate != null && (!"".equals(signDate))) {
					System.out.println("signDate=" + signDate);
					list = baseDAO.getPmContractsDAO().findBySignDate(signDate,
							projectsId);
				} else if (jiaFang != null && (!"".equals(jiaFang))) {
					list = baseDAO.getPmContractsDAO().findByJiaFang(jiaFang,
							projectsId);
				} else if (yiFang != null && (!"".equals(yiFang))) {
					list = baseDAO.getPmContractsDAO().findByYiFang(yiFang,
							projectsId);
				} else {
					list = baseDAO.getPmContractsDAO().findByProjectId(
							projectsId);
				}
			}
			List result = new ArrayList();
			if (list == null) {
				ListRange listRange = new ListRange();
				listRange.setData(result.toArray());
				listRange.setTotalSize(0);
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}

			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmContract(String temp, Map formMap) {
		PmContracts pu = new PmContracts();
		try {
			BeanUtils.populate(pu, formMap);
			PmContracts p = getPmContract(temp);

			if (p != null) {
				p = baseDAO.getPmContractsDAO().copyEntity(p, pu);
				baseDAO.getPmContractsDAO().save(p);
			} else {
				pu.setConsId(getConSeq());
				baseDAO.getPmContractsDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmContract(String temp) {
		try {
			PmContracts p = getPmContract(temp);
			if (p != null) {
				baseDAO.getPmContractsDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmContracts getPmContract(String temp) {
		PmContracts p = new PmContracts();
		List uList = baseDAO.getPmContractsDAO().findByConsId(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmContracts) uList.get(0);
			}
		}
		return p;
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	// **************************************end*****************************************

	public ListRange getPmEnds(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmEndDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmEnd(String temp, Map formMap) {
		PmEnd pu = new PmEnd();
		try {
			BeanUtils.populate(pu, formMap);
			PmEnd p = getPmEnd(temp);

			if (p != null) {
				p = baseDAO.getPmEndDAO().copyEntity(p, pu);
				baseDAO.getPmEndDAO().save(p);
			} else {
				baseDAO.getPmEndDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmEnd(String temp) {
		try {
			PmEnd p = getPmEnd(temp);
			if (p != null) {
				baseDAO.getPmEndDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmEnd getPmEnd(String temp) {
		PmEnd p = new PmEnd();
		List uList = baseDAO.getPmEndDAO().findByContractName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmEnd) uList.get(0);
			}
		}
		return p;
	}

	// **************************************error*****************************************

	public ListRange getPmErrors(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmErrorDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmError(String temp, Map formMap) {
		PmError pu = new PmError();
		try {
			BeanUtils.populate(pu, formMap);
			PmError p = getPmError(temp);
			if (p != null) {
				p = baseDAO.getPmErrorDAO().copyEntity(p, pu);
				baseDAO.getPmErrorDAO().save(p);
			} else {
				baseDAO.getPmErrorDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmError(String temp) {
		try {
			PmError p = getPmError(temp);
			if (p != null) {
				baseDAO.getPmErrorDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmError getPmError(String temp) {
		PmError p = new PmError();
		List uList = baseDAO.getPmErrorDAO().findByTitle(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmError) uList.get(0);
			}
		}
		return p;
	}

	// **************************************check*****************************************

	public ListRange getPmChecks(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmCheckDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmCheck(String temp, Map formMap) {
		PmCheck pu = new PmCheck();
		try {
			BeanUtils.populate(pu, formMap);
			PmCheck p = getPmCheck(temp);

			if (p != null) {
				p = baseDAO.getPmCheckDAO().copyEntity(p, pu);
				baseDAO.getPmCheckDAO().save(p);
			} else {
				baseDAO.getPmCheckDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmCheck(String temp) {
		try {
			PmCheck p = getPmCheck(temp);
			if (p != null) {
				baseDAO.getPmCheckDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmCheck getPmCheck(String temp) {
		PmCheck p = new PmCheck();
		List uList = baseDAO.getPmCheckDAO().findByTitle(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmCheck) uList.get(0);
			}
		}
		return p;
	}

	// **************************************ConProcess*****************************************

	public ListRange getPmConProcess(HttpServletRequest request, Map condition) {

		int rows, start, limit;
		String consId = "";
		String projectsId = request.getSession().getAttribute("projectsId")
				.toString();
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
				consId = parseString(condition.get("consId"));
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				if ("ALL".equals(consId)) {
					list = baseDAO.getPmConProcessDAO().findByProject(
							projectsId);
				} else {
					list = baseDAO.getPmConProcessDAO()
							.findByConsIDAndProjectId(consId, projectsId);
				}
			}
			List result = new ArrayList();
			if (list == null || list.size() == 0) {
				ListRange listRange = new ListRange();
				listRange.setData(result.toArray());
				listRange.setTotalSize(0);
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}

			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmConProcess(HttpServletRequest request, Map formMap) {
		PmConProcess pu = new PmConProcess();
		try {
			BeanUtils.populate(pu, formMap);
			String projectsId = request.getSession().getAttribute("projectsId")
					.toString();
			String consId = request.getSession().getAttribute("consId")
					.toString();
			pu.setProjectId(projectsId);
			pu.setConsId(consId);
			baseDAO.getPmConProcessDAO().save(pu);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// **************************************ConDep*****************************************

	public ListRange getPmConDep(HttpServletRequest request, Map condition) {

		int rows, start, limit;
		String consId = "";
		String projectsId = request.getSession().getAttribute("projectsId")
				.toString();
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
				consId = parseString(condition.get("consId"));
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				if ("ALL".equals(consId)) {
					list = baseDAO.getPmConDepDAO().findByProjectId(projectsId);
				} else {
					list = baseDAO.getPmConDepDAO().findByConsIDAndProjectId(
							consId, projectsId);
				}
			}
			List result = new ArrayList();
			if (list == null || list.size() == 0) {
				ListRange listRange = new ListRange();
				listRange.setData(result.toArray());
				listRange.setTotalSize(0);
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}

			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmConDep(HttpServletRequest request, Map formMap) {
		PmConDep pu = new PmConDep();
		try {
			BeanUtils.populate(pu, formMap);
			String projectsId = request.getSession().getAttribute("projectsId")
					.toString();
			String consId = request.getSession().getAttribute("consId")
					.toString();
			pu.setProjectId(projectsId);
			pu.setConsId(consId);
			baseDAO.getPmConDepDAO().save(pu);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
